package com.atguigu.campusintelligentassistantsystem.service;

import com.atguigu.campusintelligentassistantsystem.entity.CampusCard;
import com.atguigu.campusintelligentassistantsystem.entity.CampusCardTransaction;
import com.atguigu.campusintelligentassistantsystem.entity.TransactionType;
import com.atguigu.campusintelligentassistantsystem.repository.CampusCardRepository;
import com.atguigu.campusintelligentassistantsystem.repository.CampusCardTransactionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class CampusCardService {

    @Autowired
    private CampusCardRepository campusCardRepository;

    @Autowired
    private CampusCardTransactionRepository transactionRepository;

    /**
     * 根据学生ID获取校园卡信息
     * @param studentId 学生ID
     * @return 校园卡信息
     */
    public CampusCard getCampusCardByStudentId(Long studentId) {
        Optional<CampusCard> card = campusCardRepository.findByStudentId(studentId);
        return card.orElse(null);
    }

    /**
     * 创建校园卡
     * @param studentId 学生ID
     * @param initialBalance 初始余额
     * @return 校园卡信息
     */
    public CampusCard createCampusCard(Long studentId, BigDecimal initialBalance) {
        CampusCard card = new CampusCard();
        card.setStudentId(studentId);
        card.setBalance(initialBalance);
        card.setLastRechargeTime(LocalDateTime.now());
        return campusCardRepository.save(card);
    }

    /**
     * 校园卡充值
     * @param cardId 校园卡ID
     * @param amount 充值金额
     * @return 更新后的校园卡信息
     */
    public CampusCard recharge(Long cardId, BigDecimal amount) {
        Optional<CampusCard> cardOpt = campusCardRepository.findById(cardId);
        if (cardOpt.isPresent()) {
            CampusCard card = cardOpt.get();
            card.setBalance(card.getBalance().add(amount));
            card.setLastRechargeTime(LocalDateTime.now());
            
            // 保存充值后的校园卡信息
            CampusCard updatedCard = campusCardRepository.save(card);
            
            // 记录交易
            CampusCardTransaction transaction = new CampusCardTransaction();
            transaction.setCardId(cardId);
            transaction.setAmount(amount);
            transaction.setBalanceAfter(card.getBalance());
            transaction.setTransactionType(TransactionType.RECHARGE);
            transaction.setDescription("校园卡充值");
            transaction.setTransactionTime(LocalDateTime.now());
            transactionRepository.save(transaction);
            
            return updatedCard;
        }
        return null;
    }

    /**
     * 校园卡消费
     * @param cardId 校园卡ID
     * @param amount 消费金额
     * @return 更新后的校园卡信息
     */
    public CampusCard consume(Long cardId, BigDecimal amount) {
        Optional<CampusCard> cardOpt = campusCardRepository.findById(cardId);
        if (cardOpt.isPresent()) {
            CampusCard card = cardOpt.get();
            // 检查余额是否充足
            if (card.getBalance().compareTo(amount) >= 0) {
                card.setBalance(card.getBalance().subtract(amount));
                
                // 保存消费后的校园卡信息
                CampusCard updatedCard = campusCardRepository.save(card);
                
                // 记录交易
                CampusCardTransaction transaction = new CampusCardTransaction();
                transaction.setCardId(cardId);
                transaction.setAmount(amount);
                transaction.setBalanceAfter(card.getBalance());
                transaction.setTransactionType(TransactionType.CONSUME);
                transaction.setDescription("校园卡消费");
                transaction.setTransactionTime(LocalDateTime.now());
                transactionRepository.save(transaction);
                
                return updatedCard;
            }
        }
        return null;
    }

    /**
     * 获取校园卡交易记录
     * @param cardId 校园卡ID
     * @return 交易记录列表
     */
    public List<CampusCardTransaction> getTransactionHistory(Long cardId) {
        return transactionRepository.findByCardIdOrderByTransactionTimeDesc(cardId);
    }
}